package com.image.compress;

import android.content.Context;
import android.os.Environment;

import java.io.File;
import java.io.IOException;

/**
 * File工具类
 */

class FileUtil {
    /**
     * 防止重复
     */
    private static int temp = 1;

    /**
     * 创建图片的路径
     */
    public static File createFilePath(Context context) {
        File directory = null;
        if (checkSDExist()) {
            directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
        }
        if (directory == null) {
            context.getCacheDir();
        }
        if (!directory.exists()) {
            directory.mkdirs();
        }
        File photoFile = new File(directory, getFileName());
        if (photoFile != null && !photoFile.exists()) {
            try {
                photoFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return photoFile;

    }


    public static String getFileName() {
        long time = System.currentTimeMillis() + temp++;
        return "IMG_" + (time) + ".jpg";
    }

    // 检查是否SDK准备好
    public static boolean checkSDExist() {
        return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
    }
}
